package com.bc.bcbuyerapihttp.controller;

import com.bc.bcapi.service.UserBuyerPassportService;
import com.bc.bcbuyerapihttp.common.UserContext;
import com.bc.bccore.webconfig.WebConfig;
import com.bc.bcmodel.BuyerUserInfo;
import com.bc.bcmodel.req.UserActivateReq;
import com.bc.bcmodel.req.UserPasswordUpdateReq;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Name UserPassportController
 * @Description TODO
 * @Author douhaichao
 * @Date 2018/9/27 10:58
 * @Version 1.0
 **/
@RestController
@Api("修改密码/忘记密码/激活相关")
@RequestMapping(value = "userpassport")
public class UserPassportController {

    @Autowired
    UserBuyerPassportService userBuyerPassportService;

    @ApiOperation(value = "注册用户激活")
    @RequestMapping(value = "activate", method = RequestMethod.GET)
    public void activate(UserActivateReq req, HttpServletResponse response) {
        userBuyerPassportService.activate(req);
        try {
            response.sendRedirect(WebConfig.getSitePath() + "/pluto/success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("修改密码")
    @RequestMapping(value = "updatepwd", method = RequestMethod.POST)
    public JsonResult updatePwd(@RequestBody UserPasswordUpdateReq req,
                                HttpServletRequest request) {
        BuyerUserInfo t_user_buyer_infoModel = UserContext.getCurrentContext().getLoginInfo();
        if (t_user_buyer_infoModel == null) {
            return JsonResult.get(HttpReturnRnums.NOT_LOGIN);
        }
        req.setUser_id(t_user_buyer_infoModel.getUser_id());
        return userBuyerPassportService.updatePwd(req, request);
    }

    @ApiOperation("激活修改密码")
    @RequestMapping(value = "updatepwdactivate", method = RequestMethod.GET)
    public void updatePwdActivate(UserActivateReq req, HttpServletResponse response) {
        userBuyerPassportService.updatePwdActivate(req);
        try {
            response.sendRedirect(WebConfig.getSitePath() + "/pluto/success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("忘记密码")
    @RequestMapping(value = "forgotpwd", method = RequestMethod.POST)
    public JsonResult forgotPwd(@RequestBody UserPasswordUpdateReq req,
                                HttpServletRequest request) {
        return userBuyerPassportService.forgotPwd(req, request);
    }

    @ApiOperation(value = "获取激活忘记密码")
    @RequestMapping(value = "getforgotpwdactivate", method = RequestMethod.GET)
    public JsonResult getForgotPwdActivate(UserActivateReq req, HttpServletResponse response) {
        //忘记密码的时候应该让小达给一个页在，我后让小达调用这个接口，
        return userBuyerPassportService.getForgotPwdActivate(req);
//        try {
//            response.sendRedirect(WebConfig.getSitePath() + "/pluto/success.html");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    @ApiOperation(value = "设置激活忘记密码")
    @RequestMapping(value = "setforgotpwdactivate", method = RequestMethod.GET)
    public void setForgotPwdActivate(UserPasswordUpdateReq req, HttpServletResponse response) {
        JsonResult js = userBuyerPassportService.setForgotPwdActivate(req);
//        if (js.getCode() == HttpReturnRnums.Success.value()) {
        try {
            response.sendRedirect(WebConfig.getSitePath() + "/pluto/success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
//        }
    }

}
